﻿
' Klasse für die Verwaltung diverser System Properties. Ist so designt, das Später ev properties aus einem File einglesen werden könnten.
' Autor: Benjamin
Public Class PropertiesHandler
    Private Class Prop
        Public name As String
        Public value As String
    End Class

    Private Shared filename As String = Application.StartupPath & "\zalia.properties"

    Private Shared instance As PropertiesHandler = New PropertiesHandler()

    Private properties As New List(Of Prop)

    Public Sub New()
        readPropertiesFromFile()
        'put("db.location", "D:\Application\Visual Basic\Projects\Zalia\Zalia\src\resources\Zalia.accdb")
        put("db.connectionstring", "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & getV("db.location") & "")
    End Sub

    Public Shared Function getInstance() As PropertiesHandler
        Return instance
    End Function

    ' shared für aussen:
    Public Shared Function getValue(ByRef name As String) As String
        Return PropertiesHandler.getInstance().getV(name)
    End Function

    ' für intern
    Public Function getV(ByRef name As String) As String
        For Each p As Prop In properties
            If p.name = name Then
                Return p.value
            End If
        Next
        Return Nothing
    End Function

    Public Sub put(ByRef name As String, ByRef value As String)
        Dim p As Prop = New Prop()
        p.name = name
        p.value = value
        properties.Add(p)
    End Sub


    Private Sub readPropertiesFromFile()
        If My.Computer.FileSystem.FileExists(filename) Then
            Dim content As String = My.Computer.FileSystem.ReadAllText(filename)
            Dim lines() As String = content.Split(vbCrLf)

            For Each line As String In lines
                processLine(line)
            Next
        Else
            createPropertiesFile()
        End If
    End Sub

    Private Sub processLine(ByVal line As String)
        ' comments
        If line.StartsWith("#") Then
            Exit Sub
        End If
        Dim tokens() As String = line.Split("=")
        ' not valid.. continue!
        If tokens.Length <> 2 Then
            Exit Sub
        End If
        ' name hat noch ein vbcrlf vornedrann...
        Dim name As String = Trim(tokens(0)).Substring(1)
        Dim value As String = Trim(tokens(1))
        put(name, value)
    End Sub

    Private Sub createPropertiesFile()
        My.Computer.FileSystem.WriteAllText(filename, "# Zalia-Parameter:" & vbCrLf & "db.location = ???" & vbCrLf & vbCrLf & "# Timeout zwischen des Refreshs des Gesamten Datenbestands (damit alle Benutzer immer den Aktuellsten Stand haben) (in ms):" & vbCrLf & "db.refreshtime = ???", False)
    End Sub

    Public Function checkForAllProperties() As Boolean
        Dim result As Boolean = True
        'db.location:
        result = result And checkProperty("db.location")
        'refreshTime
        result = result And checkProperty("db.refreshtime")
        Return result
    End Function

    Private Function checkProperty(ByVal name As String) As Boolean
        Dim result As String = getV(name)
        If result Is Nothing Then
            Return False
        ElseIf result = "???" Then
            Return False
        End If
        Return True
    End Function

    Public Sub exitWhileNotEnoughProperties()
        MsgBox("Fehler beim Starten der Applikation: Mindestens ein Parameter wurde nicht angegeben. Konfigurationsfile öffnen?", vbCritical, "Fehler")
        System.Diagnostics.Process.Start(filename)
        Application.Exit()
    End Sub

End Class